package br.com.fiap.vendas.business.dao.impl;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.fiap.vendas.business.dao.interfaces.ClienteDAO;
import br.com.fiap.vendas.business.entity.ClienteEntity;


@Local(ClienteDAO.class)
@Stateless
public class ClienteDAOImpl implements ClienteDAO {

    @PersistenceContext(unitName = "vendaDS")
    private EntityManager em;


    @Override
    public void inserirCliente(ClienteEntity entity) {
        em.persist(entity);

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ClienteEntity> listarClientes() {
        Query query = em.createQuery(" select c from Cliente c ");
        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ClienteEntity> buscarClientesComPedidos() {
        Query query = em.createQuery(" select c from Cliente c " +
                " where exists (select p from Pedido p where p.cliente = c) ");
        return query.getResultList();
    }

    @Override
    public ClienteEntity localizarCliente(Integer id) {
        return em.find(ClienteEntity.class, id);
    }

}
